package com.itheima.jobs;

import com.itheima.constant.RedisConstant;
import com.itheima.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.Set;

/**
 * @author ：yeshenyue
 * @date ：Created in 2019/12/3
 * @description ：自定义定时清理垃圾图片
 * @version: 1.0
 */
public class ClearImgJob {
    @Autowired
    private JedisPool jedisPool;
    public void clearImg(){
        Jedis jedis = jedisPool.getResource();
        Set<String> set = jedis.sdiff(RedisConstant.SETMEAL_PIC_RESOURCES, RedisConstant.SETMEAL_PIC_DB_RESOURCES);
        //根据Redis中保存的两个set集合进行差值计算  获得垃圾图片名称集合
        if (set != null && set.size() > 0){
            //增强for  快捷键 iter
            for (String imgName : set) {
                //删除七牛云中的图片
                QiniuUtils.deleteFileFromQiniu(imgName);
                System.out.println("定时任务,从七牛云中删除图片:" + imgName);
                //从redis中删除图片名称
                jedis.srem(RedisConstant.SETMEAL_PIC_RESOURCES,imgName);
                System.out.println("定时任务,从redis中移除数据:" + imgName);
            }
        }
        jedis.close();
    }

}
